package org.lyq.erp.process.freighttemp.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.lyq.erp.process.freight.mapper.TblWxFreightsMapper;
import org.lyq.erp.process.freight.pojo.TblWxFreights;
import org.lyq.erp.process.freighttemp.mapper.TblWxFreightTemplateMapper;
import org.lyq.erp.process.freighttemp.pojo.TblWxFreightTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TblWxFreightTemplateServiceImpl implements TblWxFreightTemplateService {
	@Resource
	private TblWxFreightTemplateMapper tblWxFreightTemplateMapper;
	@Resource
	private TblWxFreightsMapper tblWxFreightsMapper;

	public List<TblWxFreightTemplate> selectAllFreightTemplate() {
		return this.tblWxFreightTemplateMapper.getAllFreightTemplate();
	}

	@Transactional
	public void insert(TblWxFreightTemplate freightTemp) {
		freightTemp.setFreightTempCreatetime(Long.valueOf(System.currentTimeMillis()));
		freightTemp.setFreightTempStatus(Integer.valueOf(1));
		this.tblWxFreightTemplateMapper.insertSelective(freightTemp);

		InsertTblWxFreights(freightTemp);
	}

	@Transactional
	public void update(TblWxFreightTemplate freightTemp) {
		this.tblWxFreightTemplateMapper.updateByPrimaryKeySelective(freightTemp);
		this.tblWxFreightsMapper.deleteFreightByFreightTempId(freightTemp.getFreightTempId());

		InsertTblWxFreights(freightTemp);
	}

	public TblWxFreightTemplate selectByFreightTempId(Integer freightTempId) {
		return this.tblWxFreightTemplateMapper.selectByPrimaryKey(freightTempId);
	}

	@Transactional
	public void delete(Integer freightTempId) {
		TblWxFreightTemplate freightTemp = new TblWxFreightTemplate();
		freightTemp.setFreightTempId(freightTempId);
		freightTemp.setFreightTempStatus(Integer.valueOf(0));
		this.tblWxFreightTemplateMapper.updateByPrimaryKeySelective(freightTemp);
	}

	private void InsertTblWxFreights(TblWxFreightTemplate freightTemp) {
		String[] addCodes = freightTemp.getAddCodes();
		String[] addCodeNames = freightTemp.getAddCodeNames();
		BigDecimal[] fhtFitPriAmts = freightTemp.getFreightFirstPriorityAmts();
		BigDecimal[] frtCinWhtAmts = freightTemp.getFreightContinWeightAmts();

		List<TblWxFreights> list = new ArrayList<TblWxFreights>();
		Map<String, Object> map = new HashMap<String, Object>();

		TblWxFreights tblWxFreights = null;
		for (int i = 0; i < addCodes.length; i++) {
			String[] addCode = addCodes[i].split("==");
			String[] addCodeName = addCodeNames[i].split("==");
			for (int j = 0; j < addCode.length; j++) {
				tblWxFreights = new TblWxFreights();
				tblWxFreights.setFreightAreaid(addCode[j]);
				tblWxFreights.setFreightAreaName(addCodeName[j]);
				tblWxFreights.setFreightFirstPriorityAmt(fhtFitPriAmts[i]);
				tblWxFreights.setFreightContinWeightAmt(frtCinWhtAmts[i]);
				list.add(tblWxFreights);
			}
		}
		map.put("freightTempId", freightTemp.getFreightTempId());
		map.put("list", list);
		this.tblWxFreightsMapper.insertFreightList(map);

		addCodes = null;
		addCodeNames = null;
		fhtFitPriAmts = null;
		frtCinWhtAmts = null;
		list = null;
		map = null;
	}
}
